home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 353_01 / chap11.txt < prev    next >
Text File  |  1992-01-18  |  16KB  |  350 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                        Chapter 11
  8.                                            MORE VIRTUAL FUNCTIONS
  9.  
  10. This chapter will actually be a continuation of the topics covered
  11. in the last chapter but this will be a fuller explanation of what
  12. virtual functions are and how they can be used in a program.  We
  13. will present a simple database program with a virtual function to
  14. show how it can be used, then we will go on to illustrate a more
  15. complex use of the virtual function in a manner that finally
  16. illustrates its utility and reason for existence.
  17.  
  18.  
  19. HOW TO START AN OOP PROJECT
  20. _________________________________________________________________
  21.  
  22. The observant student will notice that we begin our use of object
  23. oriented programming by identifying an object, or in this case a
  24. class of objects and even some subordinate objects, which we
  25. completely define.  When we get to the main program we then have
  26. a simple job with the remaining needs and they are completed using
  27. standard procedural programming techniques which we are familiar
  28. with.  This is the way to begin any object oriented programming
  29. project, by first identifying a few objects that can be separated
  30. conveniently from the rest of the code, programming them, then
  31. writing the main program.  It should be added that, for your first
  32. project using objects, do not try to make everything an object.
  33. Select a few objects and after gaining experience with object
  34. oriented programming techniques, use more objects on future
  35. projects.  Most programmers use too many objects for their first
  36. project and write very obtuse, unreadable code.
  37.  
  38.  
  39. THE PERSON HEADER FILE
  40. _________________________________________________________________
  41.  
  42. Examine the file named PERSON.H for the          ================
  43. definition file for the person class.  This          PERSON.H
  44. class definition should cause you no problem to  ================
  45. understand since there is nothing new here.  The
  46. only thing that should be mentioned about this
  47. class is that the protected mode is used for the variables so that
  48. they are readily available in the derived classes which will
  49. inherit this class.
  50.  
  51. THE PERSON IMPLEMENTATION
  52. _________________________________________________________________
  53.  
  54. The implementation for the person class is given here and it is a
  55. little strange in the way it is written and used.  The intent of
  56. this program is that the virtual method named display() in this
  57.  
  58.                                                         Page 11-1
  59.  
  60.                               Chapter 11 - More Virtual Functions
  61.  
  62.  
  63. file will never be used, but it is required by   ================
  64. the C++ compiler to be used for a default in        PERSON.CPP
  65. case some of the subclasses do not have this     ================
  66. function available.  In the main program we will
  67. be careful to never call this function due to
  68. the nature of the program we are writing.  Keep in mind that C++
  69. requires an implementation of all virtual functions even if they
  70. are never used.  In this case the message is obviously intended to
  71. be output as an error message.
  72.  
  73. Be sure to compile this program prior to going on to the next class
  74. definitions.
  75.  
  76.  
  77. THE SUPERVISOR HEADER
  78. _________________________________________________________________
  79.  
  80. The file named SUPERVSR.H contains the class     ================
  81. definitions for the three derived classes,          SUPERVSR.H
  82. supervisor, programmer, and secretary.  These    ================
  83. were all placed in a single file for two
  84. reasons.  The first reason is to simply
  85. illustrate to you that this can be done, and secondly, to allow
  86. some of the files to be combined on the disk and to require fewer
  87. compilations by you prior to executing the resulting program.  This
  88. is actually a good way to combine these files since they are all
  89. derived classes of a common class.  It is a matter of style or
  90. personal taste.
  91.  
  92. You will notice that all three of these classes contain a method
  93. named display() and all have the same return value of void, and all
  94. have the same number of parameters as the parent class's method of
  95. the same name.  All of this equality is required because they will
  96. all be called by the same call statement.  You will also notice
  97. that the other method in each class has the same name, but
  98. different numbers and types of formal parameters which prevents
  99. this method from being used as a virtual method.
  100.  
  101. The remainder of this file is simple and you should be able to read
  102. the code and understand it completely.  Once again, this file
  103. cannot be compiled or executed.
  104.  
  105.  
  106.  
  107. THE SUPERVISOR IMPLEMENTATION
  108. _________________________________________________________________
  109.  
  110. The file named SUPERVSR.CPP contains the         ================
  111. implementation for the three classes.  If you      SUPERVSR.CPP
  112. spend a little time studying the code, you will  ================
  113. find that each of the methods named init_data()
  114. simply initializes all fields to those passed in
  115. as the actual arguments in a very simple manner.
  116.  
  117.                                                         Page 11-2
  118.  
  119.                               Chapter 11 - More Virtual Functions
  120.  
  121.  
  122. The method named display(), however, outputs the stored data in
  123. different ways for each class since the data is so different in
  124. each of the classes.  Even though the interface to these three
  125. methods is identical, the actual code is significantly different.
  126. There is no reason code besides output could not have been used,
  127. but the output is so visible when the program is executed that it
  128. was chosen for this illustration.
  129.  
  130. This file should be compiled at this time in preparation for the
  131. next example program which will use all four classes as defined in
  132. these four files.
  133.  
  134.  
  135. THE FIRST CALLING PROGRAM
  136. _________________________________________________________________
  137.  
  138. The file named EMPLOYEE.CPP is the first program ================
  139. that uses the classes developed in this chapter,   EMPLOYEE.CPP
  140. and you will find that it is a very simple       ================
  141. program.
  142.  
  143. We begin with an array of ten pointers, each pointing to the base
  144. class.  As you recall from the last chapter, this is very important
  145. when using virtual functions, the pointer must point to the base
  146. class.  The pointers that will be stored in this array will all
  147. point to objects of the derived classes however.  When we use the
  148. resulting pointers to refer to the methods, the system will choose
  149. the method at run time, not at compile time as nearly all of our
  150. other programs have been doing.
  151.  
  152. We allocate six objects in lines 16 through 39, initialize them to
  153. some values using the methods named init_data(), then assign the
  154. pointers to the members of the array of pointers to person.
  155. Finally, in lines 41 and 42, we call the methods named display()
  156. to display the stored data on the monitor.  You will notice that
  157. even though we only use one method call in line 42, we actually
  158. send messages to each of the three methods named display() in the
  159. subclasses.  This is true dynamic binding because if we were to
  160. change the values of some of the pointers in the array, we would
  161. then call different methods with the same pointers.
  162.  
  163. Be sure to compile and execute this program before continuing on
  164. in this chapter.  You will recall that the linking step requires
  165. you to combine several files in order to satisfy all system calls.
  166. After you have done that, we will use the same objects in another
  167. way to show how they can be reused.
  168.  
  169. A PURE VIRTUAL FUNCTION
  170. _________________________________________________________________
  171.  
  172. The pure virtual function is also available in the C++ toolbox of
  173. possible constructs.  You can use a pure virtual function in the
  174.  
  175.                                                         Page 11-3
  176.  
  177.                               Chapter 11 - More Virtual Functions
  178.  
  179. present example program by changing line 10 of PERSON.H to read as
  180. follows;
  181.  
  182.      virtual void display(void) = 0;
  183.  
  184. You must then eliminate PERSON.CPP from the project or make
  185. sequence.  An implementation for a pur